/**
 * Created by pans on 15-2-15.
 * 编辑隐患
 */

angular.module('htmsMobileApp')
.controller("EditYinhuanCtrl",function($scope,$location,$routeParams,$upload,CustomHttp,RouteFilter,urlObj2Str,Authentication,serverAddr){
    console.log($routeParams.yhid);
    var yhid = $routeParams.yhid;
    $scope.yhfxfses = [{key:"自查发现",value:0},{key:"监督检查",value:1},{key:"投诉举报",value:2}];
    $scope.yhlys = [{key:"自有隐患",value:0},{key:"外来隐患",value:1}];
    $scope.zgfss = [{key:"企业自主整改",value:0},{key:"强制整改",value:1}];
    $scope.zglxs = [{key:"立即整改",value:0},{key:"限期整改",value:1},{key:"停业停产整改",value:2}];
    $scope.zgzts = [{key:"未整改",value:1},{key:"已整改",value:2},{key:"整改中",value:3}];
    $scope.d = "d2";
    CustomHttp.get("/api/yinhuan/"+yhid).success(function(data){
      $scope.yhModel = data;
      var qyid = data.entBaseInfo.qyid;
      if(data.fxyhsj){
        var sjs = data.fxyhsj.split("-");
        $scope.fxsjnf = sjs[0];
        $scope.fxsjyf = sjs[1];
        $scope.fxsjday = sjs[2];
      }
      CustomHttp.get("/api/entProAddress/findByQyid/"+qyid).success(function(data){
        $scope.jycds = data;
      })
    });

    var match = function(zgwcrq){
      return /\d{4}-\d{2}-\d{2}/.test(zgwcrq);
    };
    $scope.canAccess = function(filterValue){
      return RouteFilter.canAccess(filterValue);
    };
    $scope.mathZgwcrq = function(zgwcrq){
      if(zgwcrq ==null ||zgwcrq=="--" || zgwcrq.length ==0 || zgwcrq == "undefined" || zgwcrq==""){
        return true;
      }else
        return match(zgwcrq);
    };
    //日期年数组
    var now = new Date();
    var year = now.getFullYear() + 8;
    var yearList = [];
    for(var i = 0;i <= 30;i++){
      yearList.push(year-i);
    }
    $scope.yearList = yearList;

    //跳转到填写整改情况(如果是重大隐患则跳到填写重大隐患整改情况)
    $scope.shangBaoYinhuan2 = function(){
      if($scope.type == "zhongda"){
        $scope.d = "d4";
      }else {
        $scope.d = "d3";
      }
      $scope.yhModel.fxyhsj = $scope.fxsjnf + "-" + $scope.fxsjyf + "-"+$scope.fxsjday;
    };

    //重大隐患最后还是跳到填写一般整改情况页面，共用
    $scope.shangBaoYinhuan3 = function(){
      $scope.d = "d3";
    }

    //上传图片所用的方法
    var uploadFile = function(hdid){
      var yhfiles = $scope.yhfiles;
      var yhzgfiles = $scope.yhzgfiles;
      var fileArr = [];
      var flag;
      if(yhfiles&&yhfiles.length>0){
        flag = 0;//隐患图片
        fileArr.push(yhfiles[0]);
      }if(yhzgfiles&&yhzgfiles.length>0){
        flag = 1;//整改图片
        fileArr.push(yhzgfiles[0]);
      }
      var des;
      if(fileArr.length&&fileArr.length==2){
        des = "0,1";//两种图片都有
      }else if(fileArr.length==1){
        if(flag==0){
          des ="0";//只有隐患图片
        }else if(flag == 1){
          des = "1";//只有整改图片
        }
      }
      //开始上传图片
      $upload.upload({
        url: '/'+serverAddr.name+'/api/yinhuan/upload?hdid='+hdid + "&des=" + des,
        file: fileArr
      }).progress(function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
      }).success(function (data, status, headers, config) {
        console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
      });
    };

    //隐患上报
    $scope.shangbao = function(){
      $scope.yhModel.zgwcrq = $scope.nfzhenggai + "-" + $scope.yfzhenggai + "-"+$scope.dayzhenggai;
      if(!match($scope.yhModel.zgwcrq)){
        $scope.yhModel.zgwcrq = "";
      }
      $scope.yhModel.tbr = Authentication.getUser().name;
      $scope.yhModel.bdzt = 1;//已上报
      if($scope.type == "zhongda"){
        //重大
        $scope.yhBigModel.mbrwdqrq = $scope.mbrwdqrqY + "-" + $scope.mbrwdqrqM + "-" + $scope.mbrwdqrqD;
        $scope.yhBigModel.zgrwdqrq = $scope.zgrwdqrqY + "-" + $scope.zgrwdqrqM + "-" + $scope.zgrwdqrqD;
        $scope.yhBigModel.zlcsdqrq = $scope.zlcsdqrqY + "-" + $scope.zlcsdqrqM + "-" + $scope.zlcsdqrqD;
        $scope.yhBigModel.zgzjdqrq = $scope.zgzjdqrqY + "-" + $scope.zgzjdqrqM + "-" + $scope.zgzjdqrqD;
        $scope.yhBigModel.zgsxdqrq = $scope.zgsxdqrqY + "-" + $scope.zgsxdqrqM + "-" + $scope.zgsxdqrqD;
        $scope.yhBigModel.zgyadqrq = $scope.zgyadqrqY + "-" + $scope.zgyadqrqM + "-" + $scope.zgyadqrqD;
        $scope.yhBigModel.nrzljhrq = $scope.nrzljhrqY + "-" + $scope.nrzljhrqM + "-" + $scope.nrzljhrqD;
        if(!match($scope.yhBigModel.mbrwdqrq)){
          $scope.yhBigModel.mbrwdqrq = null;
        }
        if(!match($scope.yhBigModel.zgrwdqrq)){
          $scope.yhBigModel.zgrwdqrq = null;
        }
        if(!match($scope.yhBigModel.zlcsdqrq)){
          $scope.yhBigModel.zlcsdqrq = null;
        }
        if(!match($scope.yhBigModel.zgzjdqrq)){
          $scope.yhBigModel.zgzjdqrq = null;
        }
        if(!match($scope.yhBigModel.zgsxdqrq)){
          $scope.yhBigModel.zgsxdqrq = null;
        }
        if(!match($scope.yhBigModel.zgyadqrq)){
          $scope.yhBigModel.zgyadqrq = null;
        }
        if(!match($scope.yhBigModel.nrzljhrq)){
          $scope.yhBigModel.nrzljhrq = null;
        }
        $scope.yhBigModel.hiddenDdangerRec = $scope.yhModel;
        console.log($scope.yhBigModel);
        CustomHttp.post("/api/yinhuan/big/create",$scope.yhBigModel).success(function(data){
          uploadFile(data.hdid);
          alert("上报成功！");
          $location.path("/qiyeyinhuanByid");
        })
      }else {
        //一般
        CustomHttp.post("/api/yinhuan/create",$scope.yhModel).success(function(data){
          uploadFile(data.hdid);
          alert("上报成功！");
          $location.path("/qiyeyinhuanByid");
        })
      }

    };
    //隐患保存
    $scope.baocun = function(){
      $scope.yhModel.zgwcrq = $scope.nfzhenggai + "-" + $scope.yfzhenggai + "-"+$scope.dayzhenggai;
      if(!match($scope.yhModel.zgwcrq)){
        $scope.yhModel.zgwcrq = "";
      }
      $scope.yhModel.tbr = Authentication.getUser().name;
      $scope.yhModel.bdzt = 0;//未上报
      if($scope.type == "zhongda"){
        //重大
        $scope.yhBigModel.mbrwdqrq = $scope.mbrwdqrqY + "-" + $scope.mbrwdqrqM + "-" + $scope.mbrwdqrqD;
        $scope.yhBigModel.zgrwdqrq = $scope.zgrwdqrqY + "-" + $scope.zgrwdqrqM + "-" + $scope.zgrwdqrqD;
        $scope.yhBigModel.zlcsdqrq = $scope.zlcsdqrqY + "-" + $scope.zlcsdqrqM + "-" + $scope.zlcsdqrqD;
        $scope.yhBigModel.zgzjdqrq = $scope.zgzjdqrqY + "-" + $scope.zgzjdqrqM + "-" + $scope.zgzjdqrqD;
        $scope.yhBigModel.zgsxdqrq = $scope.zgsxdqrqY + "-" + $scope.zgsxdqrqM + "-" + $scope.zgsxdqrqD;
        $scope.yhBigModel.zgyadqrq = $scope.zgyadqrqY + "-" + $scope.zgyadqrqM + "-" + $scope.zgyadqrqD;
        $scope.yhBigModel.nrzljhrq = $scope.nrzljhrqY + "-" + $scope.nrzljhrqM + "-" + $scope.nrzljhrqD;
        if(!match($scope.yhBigModel.mbrwdqrq)){
          $scope.yhBigModel.mbrwdqrq = null;
        }
        if(!match($scope.yhBigModel.zgrwdqrq)){
          $scope.yhBigModel.zgrwdqrq = null;
        }
        if(!match($scope.yhBigModel.zlcsdqrq)){
          $scope.yhBigModel.zlcsdqrq = null;
        }
        if(!match($scope.yhBigModel.zgzjdqrq)){
          $scope.yhBigModel.zgzjdqrq = null;
        }
        if(!match($scope.yhBigModel.zgsxdqrq)){
          $scope.yhBigModel.zgsxdqrq = null;
        }
        if(!match($scope.yhBigModel.zgyadqrq)){
          $scope.yhBigModel.zgyadqrq = null;
        }
        if(!match($scope.yhBigModel.nrzljhrq)){
          $scope.yhBigModel.nrzljhrq = null;
        }
        $scope.yhBigModel.hiddenDdangerRec = $scope.yhModel;
        console.log($scope.yhBigModel);
        CustomHttp.post("/api/yinhuan/big/create", $scope.yhBigModel).success(function (data) {
          uploadFile(data.hdid);
          alert("保存成功！");
          $location.path("/qiyeyinhuanByid");
        })
      }else {
        //一般
        CustomHttp.post("/api/yinhuan/create", $scope.yhModel).success(function (data) {
          uploadFile(data.hdid);
          alert("保存成功！");
          $location.path("/qiyeyinhuanByid");
        })
      }
    };
  });
